import 'package:better_player/better_player.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';


class FullScreen extends StatefulWidget {
  final String url;

  const FullScreen({super.key, required this.url});

  @override
  State<StatefulWidget> createState() => _FullScreenState();
}

class _FullScreenState extends State<FullScreen> {
  late BetterPlayerController _controller;
  late DateTime lastTime;
  final node = FocusNode();

  @override
  void initState() {
    BetterPlayerDataSource dataSource = BetterPlayerDataSource(
      BetterPlayerDataSourceType.network,
      widget.url,
    );
    BetterPlayerControlsConfiguration controlsConfiguration =
        BetterPlayerControlsConfiguration(
      playerTheme: BetterPlayerTheme.cupertino,
      enableFullscreen: false,
      enableMute: false,
      enableOverflowMenu: false,
      enableSkips: false,
    );

    BetterPlayerConfiguration betterPlayerConfiguration =
        BetterPlayerConfiguration(
      aspectRatio: 16 / 9,
      fit: BoxFit.contain,
      autoDispose: true,
      autoDetectFullscreenAspectRatio: true,
      controlsConfiguration: controlsConfiguration,
    );

    _controller = BetterPlayerController(
      betterPlayerConfiguration,
      betterPlayerDataSource: dataSource,
    );
    lastTime = DateTime.now();
    // _controller.play();
    super.initState();
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    FocusScope.of(context).requestFocus(node);
  }

  @override
  void dispose() {
    node.dispose();
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: RawKeyboardListener(
          focusNode: node,
          autofocus: true,
          onKey: (RawKeyEvent event) {
            print('Key pressed: ${event.logicalKey}');
            switch (event.logicalKey) {
              case LogicalKeyboardKey.select:
                select();
                break;
              case LogicalKeyboardKey.arrowRight:
                right();
                break;
              case LogicalKeyboardKey.arrowLeft:
                left();
                break;
            }
          },
          child: AspectRatio(
            aspectRatio: 16 / 9,
            child: BetterPlayer(
              controller: _controller,
            ),
          ),
        ),
      ),
    );
  }

  void select() {
    print('DateTime.now().second ${DateTime.now()}');
    print('lastTime ${lastTime}');
    if (DateTime.now().difference(lastTime) > Duration(seconds: 1)) {
      setState(() {
        lastTime = DateTime.now();
      });
      print('_controller.isPlaying() ${_controller.isPlaying()!}');
      if (_controller.isPlaying()!) {
        print('pause......');
        _controller.pause();
      } else {
        print('play......');
        _controller.play();
      }
    }
  }

  Future<void> left() async {
    Duration? videoDuration = await _controller.videoPlayerController!.position;
    Duration backDuration = Duration(seconds: (videoDuration!.inSeconds - 15));
    if (DateTime.now().difference(lastTime) > Duration(seconds: 1)) {
      setState(() {
        lastTime = DateTime.now();
      });
      _controller.seekTo(backDuration);
    }
  }

  Future<void> right() async {
    Duration? videoDuration = await _controller.videoPlayerController!.position;
    Duration forwardDuration =
        Duration(seconds: (videoDuration!.inSeconds + 15));
    if (DateTime.now().difference(lastTime) > Duration(seconds: 1)) {
      setState(() {
        lastTime = DateTime.now();
      });
      _controller.seekTo(forwardDuration);
    }
  }
}
